package com.yunhe.policyfulfillment.authority.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.common.util.MD5Util;
import com.yunhe.policyfulfillment.authority.domain.*;
import com.yunhe.policyfulfillment.authority.model.AuditPolicyCompanyRegistration;
import com.yunhe.policyfulfillment.authority.model.PolicyCompanyRegistrationQueryCondition;
import com.yunhe.policyfulfillment.authority.repository.PolicyCompanyAuditProcessRepository;
import com.yunhe.policyfulfillment.authority.repository.PolicyCompanyRegistrationRepository;
import com.yunhe.policyfulfillment.authority.service.IPolicyOrgService;
import com.yunhe.policyfulfillment.authority.service.PolicyCompanyRegistrationService;
import com.yunhe.policyfulfillment.authority.service.PolicyCompanyUserService;
import com.yunhe.policyfulfillment.constant.PolicyFulfillmentConstant;
import com.yunhe.policyfulfillment.util.MyHeaderUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yunhe.common.util.CollectionUtil;

import java.util.Date;
import java.util.List;

/**
 * @description 企业注册表
 * @author misty
 * @date 2021-09-18
 */
@Service
public class PolicyCompanyRegistrationServiceImpl implements PolicyCompanyRegistrationService {

    @Autowired
    private PolicyCompanyRegistrationRepository policyCompanyRegistrationRepository;

    @Autowired
    private MyHeaderUtil myHeaderUtil;

    @Autowired
    private PolicyCompanyAuditProcessRepository policyCompanyAuditProcessRepository;

    @Autowired
    private PolicyCompanyUserService policyCompanyUserService;

    @Autowired
    private IPolicyOrgService policyOrgService;

    private static final String SALT_M = "zcdx";


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int createPolicyCompanyRegistration (PolicyCompanyRegistration policyCompanyRegistration) throws ArgumentErrorException {
        //校验账号是否重复
        PolicyCompanyUser policyCompanyUser = policyCompanyUserService.searchPolicyCompanyUserByUserAccount(policyCompanyRegistration.getUserAccount());
        if(policyCompanyUser != null){
            throw new ArgumentErrorException("该用户名已存在，请重新输入！");
        }
        PolicyCompanyRegistration policyCompanyRegistrationByCompanyUserAccount = policyCompanyRegistrationRepository.searchPolicyCompanyRegistrationByCompanyUserAccount(policyCompanyRegistration.getUserAccount());
        if (policyCompanyRegistrationByCompanyUserAccount != null){
            throw new ArgumentErrorException("当前账号已经存在用户！");
        }
        policyCompanyRegistration.setPassword(MD5Util.encode(policyCompanyRegistration.getPassword(),SALT_M));
        policyCompanyRegistration.setApplyTime(new Date());
        return policyCompanyRegistrationRepository.createPolicyCompanyRegistrationSelective(policyCompanyRegistration);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletePolicyCompanyRegistration(Long id) {
        return policyCompanyRegistrationRepository.deletePolicyCompanyRegistrationByPrimaryKey(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePolicyCompanyRegistration(PolicyCompanyRegistration policyCompanyRegistration) {
        return policyCompanyRegistrationRepository.updatePolicyCompanyRegistrationByPrimaryKeySelective(policyCompanyRegistration);
    }


    @Override
    public PolicyCompanyRegistration findPolicyCompanyRegistrationById(Long id) {
        PolicyCompanyRegistration policyCompanyRegistration = policyCompanyRegistrationRepository.searchPolicyCompanyRegistrationByPrimaryKey(id);
        if(policyCompanyRegistration != null){
            PolicyOrg policyOrg = policyOrgService.findPolicyOrgByOrgId(policyCompanyRegistration.getOrgId());
            if(policyOrg != null){
                policyCompanyRegistration.setOrgName(policyOrg.getOrgName());
                //查询审核记录
                List<PolicyCompanyAuditProcess> policyCompanyAuditProcesses = policyCompanyAuditProcessRepository.searchPolicyCompanyAuditProcessByCompanyId(id);
                if(!CollectionUtil.isEmpty(policyCompanyAuditProcesses)){
                    policyCompanyRegistration.setPolicyCompanyAuditProcessList(policyCompanyAuditProcesses);
                    policyCompanyRegistration.setAuditOpinion(policyCompanyAuditProcesses.get(0).getAuditOpinion());
                }
            }
        }

        return policyCompanyRegistration;
    }

    @Override
    public PageResult findPolicyCompanyRegistrationByCondition(PolicyCompanyRegistrationQueryCondition policyCompanyRegistrationQueryCondition) {
        PageResult result = null;
        PageHelper.startPage(policyCompanyRegistrationQueryCondition.getPage(), policyCompanyRegistrationQueryCondition.getSize());
        List<PolicyCompanyRegistration> policyCompanyRegistrations  =  policyCompanyRegistrationRepository.findPolicyCompanyRegistrationByCondition(policyCompanyRegistrationQueryCondition);
        if(!CollectionUtil.isEmpty(policyCompanyRegistrations)){
            policyCompanyRegistrations.forEach(policyCompanyRegistration -> {
                PolicyOrg policyOrg = policyOrgService.findPolicyOrgByOrgId(policyCompanyRegistration.getOrgId());
                if(policyOrg != null){
                    policyCompanyRegistration.setOrgName(policyOrg.getOrgName());
                }
            });
            PageInfo<PolicyCompanyRegistration> pageInfo = new PageInfo<>(policyCompanyRegistrations);
            result = new PageResult(policyCompanyRegistrationQueryCondition.getPage(), policyCompanyRegistrationQueryCondition.getSize(),
                    (int) pageInfo.getTotal(), policyCompanyRegistrations);
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditPolicyCompanyRegistration(AuditPolicyCompanyRegistration auditPolicyCompanyRegistration) throws ArgumentErrorException, ObjectNotFoundException {
        PolicyUser headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("登录信息已过期，请重新登录！");
        }
        PolicyCompanyRegistration policyCompanyRegistration = policyCompanyRegistrationRepository.searchPolicyCompanyRegistrationByPrimaryKey(auditPolicyCompanyRegistration.getId());
        if(policyCompanyRegistration == null){
            throw new ObjectNotFoundException("选择的企业注册信息不存在，请重新选择！");
        }
        //审核逻辑 审核通过  将企业注册信息同步到企业用户表
        PolicyFulfillmentConstant.CompanyAuditStatus companyAuditStatus = PolicyFulfillmentConstant.CompanyAuditStatus.byValue(auditPolicyCompanyRegistration.getAuditStatus());
        switch (companyAuditStatus){
            case AuditPass:
                policyCompanyRegistration.setAuditStatus(PolicyFulfillmentConstant.CompanyAuditNode.AuditPass.value());
                //同步到企业用户
                PolicyCompanyUser policyCompanyUser = new PolicyCompanyUser();
                BeanUtils.copyProperties(policyCompanyRegistration,policyCompanyUser);
                policyCompanyUser.setId(null);
                policyCompanyUser.setAuditTime(new Date());
                policyCompanyUserService.registPolicyCompanyUser(policyCompanyUser);
                break;
            case Reject:
                policyCompanyRegistration.setAuditStatus(PolicyFulfillmentConstant.CompanyAuditNode.Reject.value());
                break;
        }
        policyCompanyRegistration.setAuditTime(new Date());
        policyCompanyRegistrationRepository.updatePolicyCompanyRegistrationByPrimaryKeySelective(policyCompanyRegistration);
        //创建审核记录
        PolicyCompanyAuditProcess policyCompanyAuditProcess = new PolicyCompanyAuditProcess();
        policyCompanyAuditProcess.setCompanyRegisterId(policyCompanyRegistration.getId());
        policyCompanyAuditProcess.setAuditOpinion(auditPolicyCompanyRegistration.getAuditOpinion());
        policyCompanyAuditProcess.setAuditStatus(auditPolicyCompanyRegistration.getAuditStatus());
        policyCompanyAuditProcess.setAuditOrg(headerUser.getOrgId());
        policyCompanyAuditProcess.setAuditPerson(headerUser.getId());
        policyCompanyAuditProcess.setAuditTime(new Date());
        policyCompanyAuditProcessRepository.createPolicyCompanyAuditProcessSelective(policyCompanyAuditProcess);
    }


}